/** 
 * File: CoursePlusCharacter.java
 * Project: CMSC 345 Course Registration System, Fall 2010
 * Author: David Chong
 * Date: 12/5/10
 * E-mail: ro62899@umbc.edu
 * This class is used to hold a Course and character; the primary purpose is
 * to hold a Course and the grade that he/she earned for the Course.
 * 
 * 
 * Class Invariant: The integer fields can not be negative and the object
 *                  references can not be null.
 */

package registeringSystem;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class CoursePlusCharacter 
{
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key theKey;
	
	@Persistent
    private Course theCourse;
	
	@Persistent
    private char theChar;
    
	
	/**
	 * Name: CoursePlusCharacter <br><br>
	 * The default constructor used to initialize the private data members.
	 * <br><br>
	 * Precondition: None <br>
	 * Postcondition: The private data fields are initialized.
	 * @param None
	 */
    public CoursePlusCharacter()
    {
    	theCourse = new Course();
    	theChar = ' ';
    } // end of default constructor()
    
    
    
    /**
     * Name: CoursePlusCharacter
     * The constructor that initializes the private data members with the
     * values passed in.
     * Precondition: None
     * Postcondition: Initializes the private data members.
     * @param theCourse - the course
     * @param theChar - the character
     * @throws NullReferenceException is thrown if the Course passed is a null 
     *         reference. 
     */
    public CoursePlusCharacter(Course theCourse, char theChar) 
                                                  throws NullReferenceException
    {
    	if(theCourse == null)
    		throw new NullReferenceException("Did not create the " +
    				                         "CoursePlusCharacter object " +
    				                         "because the passed in Course " +
    				                         "was a null reference!\n");
    	
    	this.theCourse = theCourse;
    	this.theChar = theChar;
    } // end of constructor()
    
    
    
    /** 
     * Name: CoursePlusCharacter <br><br>
     * The copy constructor that initializes the private data members with the
     * same values with as the argument.  It is a deep copy.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Performs a deep copy of the argument object.
     * @param copyThis - the object that will be copied
     * @throws NullReferenceException is thrown is when a null reference is
     *         passed in
     */
    public CoursePlusCharacter(CoursePlusCharacter copyThis) 
                                                  throws NullReferenceException
    {
    	if(copyThis == null)
    		throw new NullReferenceException("Can not copy and create User " +
    				                         "object because a null reference " +
    				                         "was passed in!\n");
    	
    	try
    	{
    		theCourse = new Course(copyThis.theCourse);
    		theChar = copyThis.theChar;
    	}
    	
    	catch(NullReferenceException nre)
    	{
    		// throw up the exception if there is one
    		throw new NullReferenceException("Can not use the " +
    				                         "CoursePlusCharacter's copy " +
    				                         "constructor because a null " +
    				                         "reference was passed in!\n"); 
    	}
    } // end of copy constructor
    
    
    
    /** 
     * Name: getTheKey <br><br>
     * An accessor method that retrieves the the key for the object in the
     * Google datastore.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Returns the key. <br>
     * @param None
     */
    public Key getTheKey()
    {
    	return theKey;
    } // end of getTheKey()
    
    
    
    /** 
     * Name: getTheCourse <br><br>
     * A accessor to retrieve theCourse field.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Returns theCourse. <br>
     * @param None
     */
    public Course getTheCourse()
    {
    	return theCourse;
    } // end of getTheCourse()
    
    
    
    /** 
     * Name: getTheChar <br><br>
     * A accessor to retrieve theChar field.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Returns theChar. <br>
     * @param None
     */
    public char getTheChar()
    {
    	return theChar;
    } // end of getTheChar()
    
    
    
    /** 
     * Name: setTheCourse <br><br>
     * A mutator to change the theCourse field.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Changes the theCourse to the value passed in.
     * <br>
     * @param theCourse - the argument that will be used to set the field
     * @throws NullReferenceException is thrown when a null reference is passed
     *         in.
     */
    public void setTheCourse(Course theCourse) throws NullReferenceException
    {
    	if(theCourse == null)
    		throw new NullReferenceException("The course was not set to the " +
    				                         "object because a null " +
    				                         "reference was passed in!\n");
    	
    	this.theCourse = theCourse;
    } // end of setTheCourse()
    
    
    
    /** 
     * Name: setTheChar <br><br>
     * A mutator to change the theChar field.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Changes the theChar to the value passed in.
     * <br>
     * @param theChar - the argument that will be used to set the field
     */
    public void setTheChar(char theChar)
    {
    	this.theChar = theChar;
    } // end of setTheChar()
    
    
    
    /** 
     * Name: toString <br><br>
     * Displays the object's current state.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Returns a String object's state. <br>
     * @param None
     */
    public String toString()
    {
    	return "The course: " + theCourse.getCourseName() + "\n" +
    	       "The character: " + theChar + "\n";
    } // end of toString()
    
    
    
    /** 
     * Name: equals <br><br>
     * Compares two objects and determines if they are equal.
     * <br><br>
     * Precondition: None <br>
     * Postcondition: Returns a boolean stating if the object are equal. <br>
     * @param otherTime - the Time that will be compared to the calling object
     */
	public boolean equals(CoursePlusCharacter compareToThis)
	{
		boolean areTheyEqual;
		
		if(compareToThis == null)
			areTheyEqual = false;
		
		else
			areTheyEqual = theCourse.equals(compareToThis.theCourse) &&
			               theChar == compareToThis.theChar;
		
		return areTheyEqual;
	} // end of equals()
} // end of CoursePlusCharacter()